package com.yami.shop.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.*;
import com.yami.shop.common.util.Arith;
import com.yami.shop.dao.UserDepositMapper;
import com.yami.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserDepositServiceImpl extends ServiceImpl<UserDepositMapper,UserDeposit> implements UserDepositService {

    @Autowired
    private UserDepositMapper userDepositMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private UserDepositService userDepositService;

    @Autowired
    private ShopTypeService shopTypeService;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private CheckAccountService checkAccountService;

    @Override
    public void depositMoney(UserDeposit userDeposit) {
         //向账户表增加充值记录
         userDepositMapper.depositMoney(userDeposit);
         String userId = userDeposit.getUserId();
         //向用户表中增加充值金额
        User user = userService.getUserByUserId(userId);
        user.setUserAccount(userDeposit.getAmount());
        userService.updateById(user);
    }

    @Override
    public UserDeposit getDepositRecordByDepositId(Long depositId) {
        return userDepositMapper.getDepositRecordByDepositId(depositId);
    }

    @Override
    @Transactional
    public User depositMoneyByCash(Long depositId) {
        UserDeposit userDeposit = userDepositService.getDepositRecordByDepositId(depositId);
        Double payAmount = 0.00;
        if(userDeposit!=null){
            payAmount = userDeposit.getAmount();
            userDeposit.setPayType(1);
            userDeposit.setStatus(1);
            userDeposit.setUpdateTime(new Date());
            userDepositService.updateById(userDeposit);
        }
        //向用户账户充值金额
        String userId = userDeposit.getUserId();
        User user = userService.getUserByUserId(userId);
        //查询当前账户有没有余额 对当前余额进行累加
        Double userAccount =  user.getUserAccount();
        if(userAccount==null){
            userAccount = 0.00;
        }
         Double currentAmount =  Arith.add(payAmount,userAccount);
         user.setUserAccount(currentAmount);
         user.setVipLevel(1);
         userService.updateById(user);
         return user;
    }

    @Override
    public UserDeposit getDepositMoneyByOrderNumber(String orderNumber) {
        return userDepositMapper.getDepositMoneyByOrderNumber(orderNumber);
    }

    @Override
    public List<UserDeposit> getUserDepositList(DateTime lessThanUpdateTime) {
        return userDepositMapper.getUserDepositList(lessThanUpdateTime);
    }

    @Override
    @Transactional
    public void updatePromoteFee(UserDeposit userDeposit) {
                userDeposit.setEnterAccountTime(new Date());
                userDeposit.setEnterAccountStatus(1);
                userDepositService.saveOrUpdate(userDeposit);

                //计算推广金额
                Double promoteFee=null;
                Long shopId = userDeposit.getShopId();
                ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
                if(shopDetail!=null){
                    Long shopTypeId = shopDetail.getShopTypeId();
                    ShopType shopType = shopTypeService.getById(shopTypeId);
                    if(shopType!=null){
                        double promoteRate = shopType.getRate();
                         promoteFee = Arith.mul(userDeposit.getAmount(),promoteRate/100);
                        //将推广费用加入商家的可提现金额中去
                        Double cashmoney = shopDetail.getCashMoney();
                        if(cashmoney==null){
                            cashmoney = 0.00;
                        }
                        Double sumCashMoney = Arith.add(cashmoney,promoteFee);
                        shopDetail.setCashMoney(sumCashMoney);
                        shopDetailService.updateById(shopDetail);
                    }
                }
                //入账
                CheckAccount checkAccount = new CheckAccount();
                checkAccount.setOrderNumber(userDeposit.getOrderNumber());
                checkAccount.setType("推广费入账");
                checkAccount.setPayAmount(userDeposit.getAmount());
                checkAccount.setTime(new Date());
                checkAccount.setShopid(userDeposit.getShopId());
                checkAccount.setPromotePrice(promoteFee);
                checkAccountService.save(checkAccount);
    }

}
